<?php

if (!defined('DIR_SEP')) {
    define('DIR_SEP', DIRECTORY_SEPARATOR);
}

if (!defined('MODULES_DIR')) {
    define('MODULES_DIR', dirname(__FILE__).DIR_SEP."..".DIR_SEP);
}

require_once(MODULES_DIR.'db.inc');
require_once(MODULES_DIR.'util.inc');
require_once(MODULES_DIR.'/feeds/feedItem.inc');

class FeedBox
{	
	function __construct()
	{
		$this->db = DB::getInstance()->getDB();
	}

	public static function get5Latest()
	{
		try
		{
			global $G_HOME;
			$result = '';
			$feedItems = FeedItem::getItems("tfi.entry_date desc",0,5);
			
			foreach($feedItems as $feedItem)
			{
				$feedText = Util::cleanTags($feedItem->content);
					
				$feedText = substr($feedText,0,min(strlen($feedText),170))."...";
				
				$feedContent = 
				$result .= '<div class="boxItem">';
				$result .= "<h2 class=\"boxTitle\"><a href='".$feedItem->getUrl()."'>".$feedItem->title."</a></h2>";
				$result .= "<ul>";
				$result .= '<li class="boxDate">'.date('d/m/Y',$feedItem->entryDate).'</li>';
				$result .= '<li class="boxClicks">'.$feedItem->viewCount.'</li>';
				$result .= '<li class="boxComments">0</li>';
				$result .= "<li class=\"boxSource\"><a href='".Util::combinePath($G_HOME,"index.php?pg=feed_source&sourceId=".$feedItem->feedId."&itemId=0")."'>".$feedItem->feedName."</a></li>";
				$result .= "</ul>";
				$result .= '<div class="readableText">'.$feedText.'</div>';
				$result .= '</div>';
			}
						
			return $result;
		}
		catch(Exception $ex)
		{
			return null;
		}
	}

	public static function get5ClickByCategory($categoryId)
	{
		try
		{
			global $G_HOME;
			$result = '';
			$feedItems = FeedItem::getCategoryItems($categoryId,"tfi.view_count desc,tfi.entry_date desc",0,5);
			
			foreach($feedItems as $feedItem)
			{
				$feedText = Util::cleanTags($feedItem->content);
					
				$feedText = substr($feedText,0,min(strlen($feedText),170))."...";
				
				$feedContent = 
				$result .= '<div class="boxItem">';
				$result .= "<h2 class=\"boxTitle\"><a href='".$feedItem->getUrl()."'>".$feedItem->title."</a></h2>";
				$result .= "<ul>";
				$result .= '<li class="boxDate">'.date('d/m/Y',$feedItem->entryDate).'</li>';
				$result .= '<li class="boxClicks">'.$feedItem->viewCount.'</li>';
				$result .= '<li class="boxComments">0</li>';
				$result .= "<li class=\"boxSource\"><a href='".Util::combinePath($G_HOME,"index.php?pg=feed_source&sourceId=".$feedItem->feedId."&itemId=0")."'>".$feedItem->feedName."</a></li>";
				$result .= "</ul>";
				$result .= '<div class="readableText">'.$feedText.'</div>';
				$result .= '</div>';
			}
			
			$result .= '</table>';
			
			return $result;
		}
		catch(Exception $ex)
		{
			return null;
		}
	}	
	
}

?>